let data = {
    pagenum: 1,
    pagesize: 2,
}

function readerArticle() {
    $.ajax({
        url: '/my/article/list',
        data: data,
        success: function (res) {

            console.log(res)
            let {
                message,
                status,
                total,
                data
            } = res
            showPage(total)
            let str = ''
            if (status === 0) {
                data.forEach(item => {
                    str += `  <tr>
                   <td>${item.title}</td>
                   <td>${item.cate_name}</td>
                   <td>${item.pub_date}</td>
                   <td>${item.state}</td>
                   <td>
                   <a href="./edit.html?id=${item.id}" class="layui-btn layui-btn-xs" >编辑</a>
                   <button type="button" class="layui-btn layui-btn-xs layui-btn-danger">删除</button>
                   </td>
               </tr>`
                })
                $('tbody').html(str)

            }


        }
    })
}
readerArticle()

function showPage(t) {
    let laypage = layui.laypage;
    laypage.render({
        elem: 'page',
        count: t,
        limit: data.pagesize,
        curr: data.pagenum,
        layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
        limits: [2, 3, 5, 10],
        jump: function (obj, first) {
            if (!first) {
                data.pagenum = obj.curr;
                data.pagesize = obj.limit;
                readerArticle()
            }
        }
    })
}

$.ajax({
    url: '/my/category/list',
    success: function (res) {

        if (res.status === 0) {
            let str = '';
            res.data.forEach(item => {
                str += `<option value="${item.id}">${item.name}</option>`;
            });
            $('#category').append(str);
            let form = layui.form;
            //render更新渲染你只需要执行 form.render(type, filter); type的值select	刷新select选择框渲染
            form.render('select');
        }

    }
})
// 筛选
$('#search').on('submit', function (e) {
    // 阻止默认提交
    e.preventDefault();
    let cate_id = $('#category').val();
    let state = $('#state').val();
    if (cate_id) {
        data.cate_id = cate_id
    } else {
        delete data.cate_id; // delete是JS中的一个关键字，用于删除 对象的属性；
    }
    if (state) {
        data.state = state
    } else {
        delete data.state;
    }

    readerArticle()

})